﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco;
using umbraco.BasePages;
using umbraco.BusinessLogic;
using umbraco.interfaces;
using umbraco.controls;
using Commerce.Common;

namespace CommerceForUmbraco.adminSection.commerce
{
    public partial class dashboardOverview : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.pane_overview.Text = "New Orders";
            this.pp_neworderslector.Text = "Filter";


            if (!IsPostBack)
            {
                ddlnewOrderSelector.Items.Clear();
                ddlnewOrderSelector.Items.Add(new ListItem("Today", "0"));
                ddlnewOrderSelector.Items.Add(new ListItem("Yesterday", "1"));
                ddlnewOrderSelector.Items.Add(new ListItem("This Week", "2"));
                ddlnewOrderSelector.Items.Add(new ListItem("This Month", "3"));                
            }

            BindData();



        }


        protected void BindData()
        {
            // get collection values
            Commerce.Common.OrderCollection orders = null;

            //Assmeble start and end dates.

            DateTime startDate;
            DateTime endDate = DateTime.Now;

            
            int year = DateTime.Today.Year;
            int month = DateTime.Today.Month;
            int day = DateTime.Today.Day;

            //int lastDayOfThisMonth = new DateTime(year, month, 1).AddMonths(1).AddDays(-1).Day;
            

          switch (ddlnewOrderSelector.SelectedValue)
          {
              case "1":// Yesterday
                  startDate = new DateTime(year, month, day).AddDays(-1);
                  endDate = new DateTime(year, month, day, 23, 59, 59).AddDays(-1);

                  break;

              case "2": //This week from Monday

                  DayOfWeek dayOfWeek = DateTime.Now.DayOfWeek;
                  int daysOffset = dayOfWeek - DayOfWeek.Monday;
                  DateTime start = DateTime.Now.AddDays(-daysOffset);

                  startDate = new DateTime(start.Year, start.Month, start.Day);
                  endDate = new DateTime(year, month, day, 23, 59, 59);
                  break;

              case "3": //This month
                  startDate = new DateTime(year, month, 1);
                  endDate = new DateTime(year, month, day, 23, 59, 59);
                  break;

              default:// Today
                  startDate = new DateTime(year, month, day);
                  endDate =  new DateTime(year, month, day,23,59,59);

                  break;
          }


          orders = new OrderCollection().Where("orderStatusID", SubSonic.Comparison.NotEquals, OrderStatus.NotProcessed).Where(Order.Columns.OrderDate, SubSonic.Comparison.GreaterOrEquals, startDate).Where(Order.Columns.OrderDate, SubSonic.Comparison.LessOrEquals, endDate).OrderByDesc("OrderDate").Load();

            gvOrders.DataSource = orders;
            gvOrders.DataBind();
        }


    }
}